package servlet.response.exercises;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-17
 * Time:0:37
 */
@WebServlet(name = "PayServlet", urlPatterns = "/payServlet")
public class PayServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int amount = parseString(req.getParameter("amount"));
        String contextPath = req.getServletContext().getContextPath();
        if (amount > 100) {
            resp.sendRedirect(contextPath + "/payok.html");
        } else {
            resp.sendRedirect(contextPath + "/pay.html");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    private Integer parseString(String s) {
        int num = 0;
        try {
            num = Integer.parseInt(s);
        } catch (NumberFormatException e) {
            System.out.println("error parsing string: " + s);
        }
        return num;
    }
}
